package inutan.game.action.rpc;

import bright.serialization.ByteBuf;
import inutan.common.constant.ConfigConstant;
import inutan.common.inner.protocol.InnerMessage;
import inutan.common.kit.InnerKit;
import luban.common.LongValue;
import luban.inner.LogicModular;
import org.ricks.common.lang.Logger;
import org.ricks.ioc.anno.RpcAction;
import org.ricks.ioc.anno.RpcMethod;
import org.ricks.net.rpc.AskPackage;
import org.ricks.orm.OrmContext;

@RpcAction
public class GameRpcController {

    @RpcMethod(rpcCmd = 30000)
    public AskPackage askLogicMark(AskPackage askPackage) {
        LogicModular logicModular = new LogicModular();
        logicModular.modularId = ConfigConstant.MODULE_ID;
        ByteBuf buf = new ByteBuf();
        logicModular.serialize(buf);
        byte[] bytes = buf.array();
        AskPackage result = askPackage.valueOf(bytes);
        result.setRequest(false);
        return result;
    }

    @RpcMethod(rpcCmd = 5000)
    public AskPackage userQuit(AskPackage askPackage) {
        /**
         * 避免阻塞业务线程
         */
        LongValue longValue = LongValue.deserializeLongValue(new ByteBuf(askPackage.getPacket()));
        final long userId = longValue.value;
        OrmContext.me().destroy(userId);
        Logger.info("用户退出游戏 userId:"+ userId);

        /**
         * 网关RPC同步调用，返回
         */
        ByteBuf buf = new ByteBuf();
        LongValue data = new LongValue();
        data.value = userId;
        longValue.serialize(buf);
        AskPackage result = askPackage.valueOf(buf.array());
        result.setRequest(false);
        return result;
    }


}
